package org.onvivals.module.system.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.onvivals.common.exception.BusinessException;
import org.onvivals.common.web.Result;
import org.onvivals.module.system.entity.SysMenu;
import org.onvivals.module.system.entity.SysRoleMenu;
import org.onvivals.module.system.service.ISysMenuService;
import org.onvivals.module.system.service.ISysRoleMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色菜单控制器
 *
 */
@Tag(name = "角色菜单管理")
@RestController
@RequestMapping("/system/role-menu")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RoleMenuController {

    private final ISysRoleMenuService roleMenuService;
    private final ISysMenuService menuService;

    @Operation(summary = "查询角色菜单")
    @GetMapping("/{id}")
    public Result<List<SysMenu>> list(@PathVariable("id") Long roleId) {
        List<SysMenu> menus = menuService.list(new LambdaQueryWrapper<SysMenu>().orderByAsc(SysMenu::getSortNumber));
        List<SysRoleMenu> roleMenus = roleMenuService.list(new LambdaQueryWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, roleId));
        for (SysMenu menu : menus) {
            menu.setChecked(roleMenus.stream().anyMatch((d) -> d.getMenuId().equals(menu.getMenuId())));
        }
        return Result.OK(menus);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Operation(summary = "修改角色菜单")
    @PutMapping("/{id}")
    public Result<?> update(@PathVariable("id") Long roleId, @RequestBody List<Long> menuIds) {
        roleMenuService.remove(new LambdaUpdateWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId, roleId));
        if (CollUtil.isNotEmpty(menuIds)) {
            List<SysRoleMenu> roleMenuList = new ArrayList<>();
            for (Long menuId : menuIds) {
                SysRoleMenu sysRoleMenu = new SysRoleMenu();
                sysRoleMenu.setRoleId(roleId);
                sysRoleMenu.setMenuId(menuId);
                roleMenuList.add(sysRoleMenu);
            }
            if (!roleMenuService.saveBatch(roleMenuList)) {
                throw new BusinessException("保存失败");
            }
        }
        return Result.OK("保存成功");
    }

    @Operation(summary = "添加角色菜单")
    @PostMapping("/{id}")
    public Result<?> addRoleAuth(@PathVariable("id") Long roleId, @RequestBody Long menuId) {
        SysRoleMenu roleMenu = new SysRoleMenu();
        roleMenu.setRoleId(roleId);
        roleMenu.setMenuId(menuId);
        if (roleMenuService.save(roleMenu)) {
            return Result.OK();
        }
        return Result.error("添加角色菜单失败");
    }

    @Operation(summary = "移除角色菜单")
    @DeleteMapping("/{id}")
    public Result<?> remove(@PathVariable("id") Long roleId, @RequestBody Long menuId) {
        if (roleMenuService.remove(new LambdaUpdateWrapper<SysRoleMenu>()
                .eq(SysRoleMenu::getRoleId, roleId).eq(SysRoleMenu::getMenuId, menuId))) {
            return Result.OK();
        }
        return Result.error("移除角色菜单失败");
    }

}
